package juc.flowcontrol.countdownlatch;

import java.util.concurrent.CountDownLatch;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

/**
 * @author fangbin
 * @desc
 * @date 2020/6/26 8:49
 */
public class CountDownLatchDemo3 {

    public static void main(String[] args) throws InterruptedException {
        CountDownLatch  start = new CountDownLatch(1);
        CountDownLatch  end = new CountDownLatch(5);

        ExecutorService executorService = Executors.newFixedThreadPool(5);

        for(int i=0;i<5;i++){
            int finalI = i+1;
            Runnable runnable = new Runnable() {
                @Override
                public void run() {
                    System.out.println("No."+finalI+"准备完毕");
                    try {
                        start.await();
                        System.out.println("No."+finalI+"开始跑步");
                       Thread.sleep((long) (Math.random()*10000));
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                    end.countDown();
                    System.out.println("No."+finalI+"到达终点");
                }
            };
            executorService.submit(runnable);

        }
        try {
            Thread.sleep(5000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        System.out.println("比赛开始");
        start.countDown();
        end.await();
        System.out.println("比赛结束");


    }
}
